package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;

import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.vo.MybatisReviewVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**
     * =========================================================新增数据============================================
     */
    /***
     * @Description： Get新增操作
     * @Author: guoxiang
     * @CreateTime: 2022/9/1 10:29
     * @param name
     * @param age
     * @param sex
     * @return:com.huike.common.core.domain.AjaxResult
     * @Version: 1.0
     */

    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveData1(@PathVariable String name,
                                @PathVariable Integer age,
                                @PathVariable String sex) {
        System.out.println(name + age + sex);
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);

//        将对象传service
        int conut = reviewService.saveData1(review);

        if (conut > 0) {
            return AjaxResult.success("成功插入：" + conut + "条数据");
        } else {
            return AjaxResult.error("新增失败！！");
        }

    }

    /***
     * @Description： post新增的方法
     * @Author: guoxiang
     * @CreateTime: 2022/9/1 11:14
     * @param mybatisReviewVO
     * @return:com.huike.common.core.domain.AjaxResult
     * @Version: 1.0
     */

    @PostMapping("/saveData")
    public AjaxResult saveData2(@RequestBody MybatisReviewVO mybatisReviewVO) {
        String name = mybatisReviewVO.getName();
        Integer age = mybatisReviewVO.getAge();
        String sex = mybatisReviewVO.getSex();

        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        int i = reviewService.saveData1(review);


        if (i > 0) {
            return AjaxResult.success("成功插入：" + i + "条数据");
        } else {
            return AjaxResult.error("新增失败！！");
        }
    }


    /**
     * =========================================================删除数据=============================================
     */

    /***
     * @Description： 根据ID进行删除
     * @Author: guoxiang
     * @CreateTime: 2022/9/1 11:25
     * @param id
     * @return:com.huike.common.core.domain.AjaxResult
     * @Version: 1.0
     */

    @DeleteMapping("/remove/{id}")
    public AjaxResult delete(@PathVariable Integer id) {

        int count = reviewService.delete(id);

        if (count > 0) {
            return AjaxResult.success("删除：" + count + "条数据");
        } else {
            return AjaxResult.error("删除失败！！");
        }
    }

    /***
     * @Description：  批量删除
     * @Author: guoxiang
     * @CreateTime: 2022/9/1 19:48
     * @param ids
     * @return:com.huike.common.core.domain.AjaxResult
     * @Version: 1.0
     */

    @DeleteMapping("/removes")
    public AjaxResult deleteByIds(@RequestBody int[] ids) {
        System.out.println("对象是"+ids);

        int count = reviewService.deleteByIds(ids);



        if (count > 0) {
            return AjaxResult.success("成功删除：" + count + "条数据");
        } else {
            return AjaxResult.error("删除失败！！");
        }
    }
    /***
     * @Description： 批量删除list实现
     * @Author: guoxiang
     * @CreateTime: 2022/9/1 20:17
     * @param ids
     * @return:com.huike.common.core.domain.AjaxResult
     * @Version: 1.0
     */

    @DeleteMapping("/removess")
    public AjaxResult deleteByIdss(@RequestBody List<Long> ids) {



//        System.out.println("对象是"+ids);

        int count = reviewService.deleteByIdss(ids);



        if (count > 0) {
            return AjaxResult.success("成功删除：" + count + "条数据");
        } else {
            return AjaxResult.error("删除失败！！");
        }
    }

    /**=========================================================修改数据=============================================*/

    /***
     * @Description： 根据ID进行修改
     * @Author: guoxiang
     * @CreateTime: 2022/9/1 11:27
     * @return:com.huike.common.core.domain.AjaxResult
     * @Version: 1.0
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody MybatisReviewVO mybatisReviewVO) {

        Long id = mybatisReviewVO.getId();
        String name = mybatisReviewVO.getName();
        Integer age = mybatisReviewVO.getAge();
        String sex = mybatisReviewVO.getSex();

        Review review = new Review().setId(id).setName(name).setAge(age).setSex(sex);
       /* review.setId(id);
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);*/

//        更新方法
        int count = reviewService.update(review);
        if (count > 0) {
            return AjaxResult.success("更新成功：" + count + "条数据");
        } else {
            return AjaxResult.error("更新失败！！");
        }
    }
    /**=========================================================查询数据=============================================*/

//    分页查
//    条件查

    /***
     * @Description： 根据ID进行查询
     * @Author: guoxiang
     * @CreateTime: 2022/9/1 14:13
     * @param id
     * @return:com.huike.common.core.domain.AjaxResult
     * @Version: 1.0
     */

    @GetMapping("/getById")
    public AjaxResult getById(Integer id) {

        Review review = reviewService.getById(id);
        Long id1 = review.getId();
        Integer age = review.getAge();
        String sex = review.getSex();
        String createBy = review.getCreateBy();
        Date createTime = review.getCreateTime();
//        这里需要改
        String updateBy = review.getUpdateBy();
        Date updateTime = review.getUpdateTime();

        //这样设置无法完成产品的要求
        MybatisReviewVO mybatisReviewVO = new MybatisReviewVO();


        return AjaxResult.success("操作成功！", review);
    }


    /***
     * @Description： 分页查询
     * @Author: guoxiang
     * @CreateTime: 2022/9/1 14:19
     * @return:com.huike.common.core.page.TableDataInfo
     * @Version: 1.0
     */

    @GetMapping("/getDataByPage")
    public TableDataInfo getDataByPage() {
//        分页信息包含在这里边
        startPage();

        List<Review> reviews = reviewService.getDataByPage();

//        对象的拷贝--此处知识练习功能，但是出的结果，下方没用
        List<MybatisReviewVO> mybatisReviewVOS = reviews.stream().map(review -> {
            MybatisReviewVO mybatisReviewVO = new MybatisReviewVO();
            BeanUtils.copyProperties(review, mybatisReviewVO);
            return mybatisReviewVO;
        }).collect(Collectors.toList());

//      返回查询到的结果集
        return getDataTable(reviews);
    }
}